﻿$(document).ready(function(){
	//alert("enter");
	$.ajax({
            type:"GET",
            url:"/update/chenb",
            dataType:"text",
            data:{ajax:"1",time:"6000000"},
            success:function(data,textStatus){
                    //成功
                    if(data.success=="1"){
                        alert("success");
                    }
                    //超时
                    if(data.success=="0"){
                        alert("timeout");
                    }
            },
            complete:function(XMLHttpRequest,textStatus){
                    if(XMLHttpRequest.readyState=="4"){
                    		//alert("complete");
                        //alert(XMLHttpRequest.responseText);
                    }
            },
            error: function(XMLHttpRequest,textStatus,errorThrown){
                        alert("error");
            }
        });
	});

function GetRoomInfo()
{
	$.get("/getroominfo",function(data){create(data);});
}

function create(data)
{
	var p =data.split(";");
	for(var i=0; i<p.length; ++i)
	{
		if(trim(p[i]) != "")
		{
			var roomname = p[i].split(",")[0];
			var roomid   = p[i].split(",")[1];
			//创建房间
			if(i == 0)
			{
				document.getElementById("room").innerHTML += "&nbsp;<input type='radio' name='room' onclick='myform.room.value="+roomid+";' checked='checked'>"+ roomname+"<br>";
			}
			else
			{
				document.getElementById("room").innerHTML += "&nbsp;<input type='radio' name='room' onclick='myform.room.value="+roomid+";'>"+ roomname+"<br>";
			}

		}
	}
	document.getElementById("room").innerHTML += "<br><br>";
}

function login(myname,gender,roomid)
{
	$.post("/",myname+","+gender+","+roomid,function(data){finish_login(data);});
}

function CleanUsr()
{
	document.myform.usr.value = '';
	document.myform.usr.focus();
}
function check()
 {
 	var myname = document.myform.usr.value;
 	myname = trim(myname);
 	var flag = checkIfTab(myname);
 	if(flag == false)
 	{
 		alert("用户名非法");
 		CleanUsr();
 		return;
 	}
	if(myname == "")
	{
		alert("用户名不能为空");
		return;
	}
	var gender = document.myform.xb.value;
	var roomid = document.myform.roomid.value;
	//document.myform.usr.value = gender+","+roomid;
	login(myname,gender,roomid);
 }

function Init()
{
	document.myform.usr.value = '';
	document.myform.usr.focus();
	GetRoomInfo();
}

function finish_login(data)
{
	var errorno = data.split(":")[0];
	var info = data.split(":")[1];
	if(parseInt(errorno) < 0)
	{
		//alert(info);
		CleanUsr();
		return;
	}
	else
	{
		//设置cookie
		var myname = document.myform.usr.value;
		var gender = document.myform.xb.value;
		var roomid = document.myform.roomid.value;
		deleteCookie("ailiaousrname");
		deleteCookie("roomid");
		deleteCookie("gender");
		setCookie("ailiaousrname",myname);
		setCookie("roomid",roomid);
		setCookie("gender",gender);
		//window.location.href = "/chat";
		alert("finish");
		//window.locate("/chat");
		window.location.href = "/chat";
	}
}